package com.login_php.actions;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.login_php.constants.SessionConstants;
import com.login_php.domain.user.JsonReturn;
import com.login_php.manager.AplicationEng;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseServiceAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{
	
	private static final long serialVersionUID = 5825757364584658303L;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected String actionError;
	protected boolean hasActionError;
	protected String actionMessage;
	protected boolean hasActionMessage;
	protected String actionSuccess;
	protected boolean hasActionSuccess;
	
	private JsonReturn loginRequire;
	
	public static final String LOGIN_REQUIRE = "LOGIN_REQUIRE";
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	protected HttpServletRequest getServletRequest() {
		return this.request;
	}
	
	protected HttpServletResponse getServletResponse() {
		return this.response;
	}

	protected AplicationEng getAplicationEng(){
		
		return (AplicationEng) getSession().getAttribute(SessionConstants.ENG_KEY);
	}
	
	@Override
	public void addActionError(String anErrorMessage) {
		if(StringUtils.isNotEmpty(anErrorMessage)){
			this.actionError = anErrorMessage;
			this.hasActionError = true;
			request.setAttribute("actionError", actionError);
			request.setAttribute("hasActionError", hasActionError);
		}
	}

	
	public void addActionSuccess(String anSuccessMessage) {
		if(StringUtils.isNotEmpty(anSuccessMessage)){
			this.actionSuccess = anSuccessMessage;
			this.hasActionSuccess = true;

			request.setAttribute("actionSuccess", actionSuccess);
			request.setAttribute("hasActionSuccess", hasActionSuccess);
		}
	}
	
	@Override
	public void addActionMessage(String aMessage) {
		if(StringUtils.isNotEmpty(aMessage)){
			this.actionMessage = aMessage;
			this.hasActionMessage= true;

			request.setAttribute("actionMessage", actionMessage);
			request.setAttribute("hasActionMessage", hasActionMessage);
		}
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getActionError() {
		return actionError;
	}

	public void setActionError(String actionError) {
		this.actionError = actionError;
	}

	public boolean isHasActionError() {
		return hasActionError;
	}

	public void setHasActionError(boolean hasActionError) {
		this.hasActionError = hasActionError;
	}

	public String getActionMessage() {
		return actionMessage;
	}

	public void setActionMessage(String actionMessage) {
		this.actionMessage = actionMessage;
	}

	public boolean isHasActionMessage() {
		return hasActionMessage;
	}

	public void setHasActionMessage(boolean hasActionMessage) {
		this.hasActionMessage = hasActionMessage;
	}

	public String getActionSuccess() {
		return actionSuccess;
	}

	public void setActionSuccess(String actionSuccess) {
		this.actionSuccess = actionSuccess;
	}

	public boolean isHasActionSuccess() {
		return hasActionSuccess;
	}

	public void setHasActionSuccess(boolean hasActionSuccess) {
		this.hasActionSuccess = hasActionSuccess;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	@Override
	public String execute() throws Exception {
		
		if(isLoginRequired()){
			if(!getAplicationEng().getUsuarioEng().isUsuarioLogado()){
				
				 loginRequire = new JsonReturn();
				
				loginRequire.setMessage("Você precisa estar logado");
				loginRequire.setSuccess(false);
				
				return LOGIN_REQUIRE;
			}
		}
		
		return doExecute();
	}
	
	public abstract String doExecute();
	
	protected boolean isLoginRequired(){return false;}
	
	public JsonReturn getLoginRequire(){return loginRequire;}
	
	private HttpSession getSessionById(final String sessionId) {
    	final ServletContext context = getServletContext();
    	final HttpSession session = (HttpSession) context.getAttribute(sessionId);
    	return session;
    }

	private ServletContext getServletContext() {
		return getRequest().getSession().getServletContext();
	}
	
	
	protected HttpSession getSession(){
		String sessionId = (String) getServletRequest().getAttribute("JSESSIONID");
		if(StringUtils.isEmpty(sessionId)){
			sessionId = "";
			if(getServletRequest().getCookies() != null 
					&& getServletRequest().getCookies().length > 0){
				for(Cookie cookie : getServletRequest().getCookies()){
					if("JSESSIONID".equalsIgnoreCase(cookie.getName())){
						sessionId = cookie.getValue();
						break;
					}
				}
			}
		}
		
		HttpSession session = getSessionById(sessionId);
		
		return (session == null ? getServletRequest().getSession() : session);
	}
}
